﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

using SchemaMonitor;
using Framework;
using System.Web.UI;

/// <ummary>
/// Summary description for CGenerate
/// </summary>
public class CGenerate
{
	public interface IDisplay
	{
		void Display(CPhotoHashList list);
		string ToHtml(int cols, int width, CPhotoHashList list);
	}


	public static readonly string FOLDER = HttpContext.Current.Server.MapPath("~/app_data/");
	public static readonly string BOOK = FOLDER + "book.htm";
	public static readonly string CLNK = FOLDER + "ChapterLink.htm";
	public static readonly string SLNK = FOLDER + "SectionLinks.htm";
	public static readonly string CHAP = FOLDER + "Chapter.htm";
	public static readonly string SECT = FOLDER + "Section.htm";
	public static string ToHtml(int bookId, CUser u)
	{
		var b = CBook.Cache.GetById(bookId);
		return ToHtml(b, u);
	}
	public static string ToHtml(CBook b, CUser u, int sectionId=0, bool noImgs=false)
	{
		var book = new CTemplate(BOOK);
		book.Replace("UserName", u.UserName);
		book.Replace("BookName", b.BookName);
		book.Replace("Links", ToHtml_Links(b, sectionId));
		book.Replace("Content", ToHtml_Content(b, u, sectionId, noImgs));
		return book.Template;
	}
	private static string ToHtml_Content(CBook b, CUser u, int sectionId, bool noImgs)
	{
		var s = new CTemplate(SECT);
		var c = new CTemplate(CHAP);
		var cc = new CTemplate(CLNK);

		var sb = new StringBuilder();
		foreach (var i in b.Sections)
		{
			if (sectionId > 0)
				if (sectionId != i.SectionId)
					continue;

			var p1 = (1 + b.Sections.IndexOf(i));
			s.Reset();
			s.Replace("SectionId", i.SectionId.ToString());
			s.Replace("SectionName", i.SectionName);
			s.Replace("Prefix", string.Concat(p1, ". "));
			s.Replace("Chapters", ToHtml_Content(i, c, u, noImgs));
			s.Replace("ChapterLinks", ToHtml_Links(i, cc));
			sb.AppendLine(s.Template);
		}
		return sb.ToString();
	}
	private static string ToHtml_Content(CSection section, CTemplate c, CUser u, bool noImgs)
	{
		var p1 = (1 + section.Book.Sections.IndexOf(section));
		var sb = new StringBuilder();
		foreach (var i in section.Chapters)
		{
			var p2 = (1 + section.Chapters.IndexOf(i));

			var id = Math.Abs(i.ChapterNodeGuid.GetHashCode()).ToString();
			c.Reset();
			c.Replace("ChapterId", id);
			c.Replace("ChapterName", i.ChapterName);
			c.Replace("SectionId", i.ChapterSectionId.ToString());
			c.Replace("Prefix", string.Concat(p1, ".", p2));

			var r = i.Node_(u).Root;
			var f = r.Fields;

			//Content
			string s = r.ContentMessageOrName;
			s = HttpUtility.HtmlEncode(s);
			s = s.Replace("\r\n", "\n").Replace("\n\n", "</p><p>").Replace("\n", "<br/>");


			//Photos
			var photos = string.Empty;
			if (r.HasPhotoIds)
			{
				var ids = r.PhotoIds;
				var usePanel = s.Length > 10000;
				photos = Photos(ids, usePanel);
				if (noImgs)
					photos = string.Empty;

				if (usePanel || ids.Count<=2)
				{
					c.Replace("PhotosTop", photos);
					photos = string.Empty;
				}
			}

			c.Replace("Content", s);
			c.Replace("PhotosBottom", photos);
			c.Replace("PhotosTop", string.Empty);

			sb.AppendLine(c.Template);
		}
		return sb.ToString();
	}

	//Links
	private static string ToHtml_Links(CBook b, int sectionId)
	{
		if (sectionId > 0)
			return string.Empty;
		var s = new CTemplate(SLNK);
		var c = new CTemplate(CLNK);

		var sb = new StringBuilder();
		foreach (var i in b.Sections)
		{
			if (sectionId > 0)
				if (i.SectionId != sectionId)
					continue;

			var p1 = (1 + b.Sections.IndexOf(i));

			s.Reset();
			s.Replace("SectionId", i.SectionId.ToString());
			s.Replace("SectionName", i.SectionName);
			s.Replace("Prefix", string.Concat(p1, ". "));
			s.Replace("Chapters", ToHtml_Links(i, c));
			sb.AppendLine(s.Template);
		}
		return sb.ToString();
	}
	private static string ToHtml_Links(CSection s, CTemplate c)
	{
		var p1 = (1 + s.Book.Sections.IndexOf(s));
		var sb = new StringBuilder();
		var ss = s.Book.Sections;
		foreach (var i in s.Chapters)
		{
			var p2 = (1 + s.Chapters.IndexOf(i));

			var id = Math.Abs(i.ChapterNodeGuid.GetHashCode()).ToString();
			c.Reset();
			c.Replace("ChapterId", id);
			c.Replace("ChapterName", i.ChapterName);
			c.Replace("SectionId", i.ChapterSectionId.ToString());
			c.Replace("Prefix", string.Concat(p1, ".", p2));
			sb.AppendLine(c.Template);
		}
		return sb.ToString();
	}


	#region Photos
	private static string Photos(List<long> ids, bool usePanel)
	{
		if (ids.Count == 0)
			return string.Empty;
		if (ids.Count == 1)
			return PhotoDiv(ids[0], 300);
		if (ids.Count == 2)
			return PhotoPanel(ids, 300, 2);

		if (usePanel)
			return PhotoPanel(ids);
		return PhotoPanelHoriz(ids);
		//return PhotoTbl(ids, 10);
	}
	private static string PhotoPanel(List<long> ids, int width = 120, int multiplier = 1)
	{
		StringBuilder sb = new StringBuilder();
		sb.Append("<div style='width:auto").Append("; float:left'>");

		foreach (var i in ids)
			sb.AppendLine(PhotoDivHoriz(i, width,"margin-bottom:5px; margin-right:20px"));

		sb.AppendLine("</div>");
		return sb.ToString().Replace("clear:both", "");
	}

	private static string PhotoPanelHoriz(List<long> ids, int height = 100)
	{
		StringBuilder sb = new StringBuilder("<div style='clear:both;display:table'>");

		foreach (var i in ids)
			sb.AppendLine(PhotoDivHoriz(i, height, "margin-bottom:5px; margin-right:20px"));

		sb.AppendLine("</div>");
		return sb.ToString();
	}


	private static string PhotoTbl(List<long> ids, int cols = 5, string width = "100%", string tableAttr = "width=100%")
	{
		StringBuilder sb = new StringBuilder("<table ");
		sb.Append(tableAttr).Append(" >");

		var rows = ids.Count / cols;
		var rem = ids.Count % cols;
		if (rem > 0)
			rows++;
		for (var i = 0; i < rows; i++)
			sb.AppendLine(PhotoRow(ids, cols, width, i));

		sb.AppendLine("</table>");
		return sb.ToString();
	}
	private static string PhotoRow(List<long> ids, int cols, string width, int row)
	{
		var offset = row * cols;
		StringBuilder sb = new StringBuilder("<tr>");
		for (var i = 0; i < cols; i++)
		{
			sb.AppendLine("<td valign=top>");
			var idx = i + offset;
			if (idx >= ids.Count)
				sb.AppendLine("&nbsp;");
			else
				sb.AppendLine(PhotoImg(ids[idx], width));
			sb.AppendLine("</td>");
		}
		sb.AppendLine("</tr>");
		return sb.ToString();
	}


	private static string PhotoDiv(long id, int width, string style="", string left = "left")
	{
		string img = PhotoImg(id, width.ToString());
		return string.Concat("<div style='float:", left, "; clear:both; width:", width ,"px; margin-right:10px; margin-bottom:10px; ", style, "'>", img, "</div>");
	}
	private static string PhotoDivHoriz(long id, int height, string style = "")
	{
		string img = PhotoImg(id, height.ToString()).Replace("width", "height");
		return string.Concat("<div style='float:left; height:", height, "px; ", style, "'>", img, "</div>");
	}
	private static string PhotoImg(long id, string width)
	{
		var url = string.Concat("http://recyclebin.mikeconnellministries.com/photo.aspx?id=", id);
		var img = string.Concat("<img src=\"", url, "\" width=\"", width, "\" />");
		var lnk = string.Concat("<a href=\"", url, "\" target=_blank>", img, "</a>");
		return lnk;
	}
	#endregion
}